package algorithm.dynamic_programming;

public class SplitSumClosedSizeHalf {


    public static int ways(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        sum /= 2;
        if (arr.length % 2 == 0) {
            return process(arr, 0, sum, arr.length / 2);
        } else {
            return Math.max(process(arr, 0, sum, arr.length / 2), process(arr, 0, sum, arr.length / 2 + 1));
        }
    }

    public static int process(int[] arr, int index, int rest, int num_rest) {
        int N = arr.length;
        if (rest < 0)
            return -1;
        if (index == arr.length || num_rest == 0)
            return 0;
        if (index == N - num_rest) {
            int process = process(arr, index + 1, rest - arr[index], num_rest - 1);
            if (process != -1)
                return arr[index] + process;
            else
                return -1;
        }
        int process1 = process(arr, index + 1, rest, num_rest);
        int process2 = process(arr, index + 1, rest - arr[index], num_rest - 1);
        if (process1 != -1) {
            return process2 == -1 ? process1 : Math.max(process1, process2 + arr[index]);
        } else if (process2 != -1) {
            return process2 + arr[index];
        } else
            return -1;
    }

    public static int ways2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        sum /= 2;
        int N = arr.length;
        int M = (N + 1) / 2;
        int[][][] dp = new int[N + 1][sum + 1][M + 1];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < sum + 1; j++) {
                for (int k = 1; k < M + 1; k++) {
                    dp[i][j][k] = -1;
                }
            }
        }

        for (int i = N - 1; i >= 0; i--) {
            for (int k = 1; k < M + 1; k++) {
                for (int j = 0; j < sum + 1; j++) {
                    if (i == N - k) {
                        int process = j - arr[i] < 0? -1 : dp[i + 1][j - arr[i]][k - 1];
                        if (process != -1)
                            dp[i][j][k] = arr[i] + dp[i + 1][j - arr[i]][k - 1];
                        else
                            dp[i][j][k] = -1;
                    } else {
                        int process1 = dp[i + 1][j][k];
                        int process2 = j - arr[i] < 0? -1 : dp[i + 1][j - arr[i]][k - 1];
                        if (process1 != -1) {
                            dp[i][j][k] = process2 == -1 ? process1 : Math.max(process1, process2 + arr[i]);
                        } else if (process2 != -1) {
                            dp[i][j][k] = process2 + arr[i];
                        } else
                            dp[i][j][k] = -1;
                    }

                }
            }
        }

        if (arr.length % 2 == 0) {
            return dp[0][sum][arr.length / 2];
        } else {
            return Math.max(dp[0][sum][arr.length / 2], dp[0][sum][arr.length / 2 + 1]);
        }
    }

    // for test
    public static int[] randomArray(int len, int value) {
        int[] arr = new int[len];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * value);
        }
        return arr;
    }

    // for test
    public static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        int maxLen = 10;
        int maxValue = 50;
        int testTime = 10000;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int len = (int) (Math.random() * maxLen);
            int[] arr = randomArray(len, maxValue);
            int ans1 = ways(arr);
            int ans2 = ways2(arr);
            if (ans1 != ans2) {
                printArray(arr);
                System.out.println(ans1);
                System.out.println(ans2);
                System.out.println("Oops!");
                break;
            }
        }
        System.out.println("测试结束");

//        int[] arr = {2, 1};
//        System.out.println(ways(arr));
//        System.out.println(ways2(arr));
    }
}
